﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace Service
{
    /// <summary>
    /// 
    /// </summary>
    public class RequestService  
    {
        /// <summary>
        /// 执行HTTP POST请求。
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <returns>HTTP响应</returns>
        public string PostJson(string url, string data)
        {
            string result = "";
            try
            {
                if (string.IsNullOrWhiteSpace(url))
                {
                    return "";
                }
                HttpWebRequest req = GetWebRequest(url, "POST");
                req.ContentType = "application/json;";
                req.Headers.Add("Authorization", "Bearer sk-09b329e4de6c4b85b20963414705234b");
                //req.Accept = "application/json";
                byte[] postData = Encoding.UTF8.GetBytes(data);
                Stream reqStream = req.GetRequestStream();
                reqStream.Write(postData, 0, postData.Length);
                reqStream.Close();
                HttpWebResponse rsp = null;
                rsp = (HttpWebResponse)req.GetResponse();
                //Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
                result = GetResponseAsString(rsp, Encoding.UTF8);
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        private HttpWebRequest GetWebRequest(String url, String method)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = method;
            return req;
        }
        /// <summary>
        /// 把响应流转换为文本。
        /// </summary>
        /// <param name="rsp">响应流对象</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>响应文本</returns>
        private string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
        {
            Stream stream = null;
            StreamReader reader = null;
            try
            {
                // 以字符流的方式读取HTTP响应
                stream = rsp.GetResponseStream();
                reader = new StreamReader(stream, encoding);
                return reader.ReadToEnd();
            }
            catch (Exception ex) 
            {
                return "";
            }
            finally
            {
                // 释放资源
                if (reader != null) reader.Close();
                if (stream != null) stream.Close();
                if (rsp != null) rsp.Close();
            }
        }

        public string Dwnload(string url)
        {
            WebClient webClient = null;
            using (webClient = new WebClient { })
            {
                var bytes = webClient.DownloadData(url);
                string urlPath = $"QDP/{DateTime.Now.ToString("yyyyMMdd")}/1.pdf";

                string filePath = $"{AppDomain.CurrentDomain.BaseDirectory}{urlPath}"; // 文件路径
                string directoryName = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                //获得字节数组

                byte[] data = bytes;
                //开始写入
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
                return urlPath;
            }
        }
    }
}
